import router from '@system.router';
import app from '@system.app';

const MILLIS_TWO_SECONDS = 2 * 1000;

var cmdno = {
    init: 0,
    close: 1,
    read: 2,
    dian: 3,
    deng: 4
}

export default {
    data: {
        title: '带式输送机控制系统',
        mydata: {
            Temp: 0,
            Hum: 0,
            Lux: 0,
            Motor: 'OFF',
            LED: 'OFF'
        },
        interval: 0
    },
    onInit() {
        app.e53ia1service({
            cmd: cmdno.init,
            success(res) {
            },
            fail(res) {
            },
            complete(res) {
            },
        })
        this.interval = setInterval(() => this.queryData(), MILLIS_TWO_SECONDS)
    },
    toback() {
        app.e53ia1service({
            cmd: cmdno.close,
            success(res) {
            },
            fail(res) {

            },
            complete(res) {

            },
        })
        clearInterval(this.interval)
        router.replace({
            uri: 'pages/index/index'
        });
    },
    dengChange() {
        let that = this
        let flag = ''
        if (this.mydata.LED == 'OFF') {
            flag = 'ON'
        }
        else {
            flag = 'OFF'
        }
        app.e53ia1service({
            cmd: cmdno.deng,
            data: flag,
            success(res) {
                that.mydata = JSON.parse(res.e53_ia1)
            },
            fail(res) {

            },
            complete(res) {

            },
        })
    },
    dianChange() {
        let that = this
        let flag = ''
        if (this.mydata.Motor == 'OFF') {
            flag = 'ON'
        }
        else {
            flag = 'OFF'
        }
        app.e53ia1service({
            cmd: cmdno.dian,
            data: flag,
            success(res) {
                that.mydata = JSON.parse(res.e53_ia1)
            },
            fail(res) {

            },
            complete(res) {

            },
        })
    },
    queryData() {
        let that = this
        app.e53ia1service({
            cmd: cmdno.read,
            data: '',
            success(res) {
                that.mydata = JSON.parse(res.e53_ia1)
            },
            fail(res) {

            },
            complete(res) {

            },
        })
    },
    exit() {
        app.terminate();
    }
}
